
#import "ComparePriceViewController.h"
#import "UINavigationItem+Customization.h"

@interface ComparePriceViewController () <UIPickerViewAccessibilityDelegate>

@end

@implementation ComparePriceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil calculator:(SavingsCalculator *)calculator
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        savingsCalculator = calculator;
        prices = [NSMutableArray array];
        NSInteger priceIntervall = 5;
        for (NSInteger i=priceIntervall; i <= 400; i += priceIntervall) {
            if (i == 200) {
                priceIntervall = 10;
            }
            [prices addObject:[NSString stringWithFormat:@"%d", i]];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UINavigationItem *item = self.navigationBar.items[0];
    [self.navigationBar applyDefaultStyle];
    UIButton *doneButton = [self.navigationBar doneButtonWith:[UIImage imageNamed:@"btn-resultat-klar-off.png"] highlight:[UIImage imageNamed:@"btn-resultat-klar-on.png"] leftCapWidth:14.0 title:_(@"Klar")];
    [doneButton addTarget:self action:@selector(donePressed) forControlEvents:UIControlEventTouchUpInside];
    item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
    [item customizeTitleInNavigationBarWithHeight:self.navigationBar.frame.size.height];
    
    [self.pickerView reloadAllComponents];
    
    float purchasePrice = [savingsCalculator purchasePriceForSubstituteAtIndex:self.subtituteIndex];
    int pickerindex = [prices indexOfObject:[NSString stringWithFormat:@"%.0f", purchasePrice]];
    [self.pickerView selectRow:pickerindex inComponent:0 animated:NO];
    
    self.costLabel.text = [NSString stringWithFormat:@"ca %.0f kr", purchasePrice];
    self.costLabel.accessibilityLabel = [self.costLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];
    self.savingsLabel.text = [NSString stringWithFormat:@"ca %.0f kr",[savingsCalculator savingForSubstituteAtIndex:self.subtituteIndex]];
    self.savingsLabel.accessibilityLabel = [self.savingsLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];
    self.yearLabel.hidden = [savingsCalculator savingForSubstituteAtIndex:self.subtituteIndex] <= 0 ? YES : NO;
    [self relayoutSavingsLabels];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(175, 93, 80, 30)];
    label.text = @" kr";
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    label.backgroundColor = [UIColor clearColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake (0,1);
    [self.pickerView insertSubview:label aboveSubview:self.pickerView.subviews[5]];
    
    self.lampLabel.text = self.lampString;
    self.yearLabel.text = [self yearLabelForLampName:self.lampString];
}

- (void)viewDidUnload
{
    [self setSavingsLabel:nil];
    [self setCostLabel:nil];
    [self setLampLabel:nil];
    [self setLampString:nil];
    [self setYearLabel:nil];
    [super viewDidUnload];
    self.navigationBar = nil;
    self.pickerView = nil;
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSString *)yearLabelForLampName:(NSString *)lampName
{
    NSString *yearLabel = nil;
    if ([lampName isEqualToString:@"LED"] || [lampName isEqualToString:@"Lågenergi"]) {
        yearLabel = @"på 10 år";
    } else if ([lampName isEqualToString:@"Halogen B"]) {
        yearLabel = @"på 3 år";
    } else if ([lampName isEqualToString:@"Halogen C"]) {
        yearLabel = @"på 2 år";
    }
    return yearLabel;
}

#pragma mark -
- (void)relayoutSavingsLabels
{
    [self.savingsLabel sizeToFit];
    CGRect frame = self.savingsLabel.frame;
    frame.origin.y = 121;
    self.savingsLabel.frame = frame;
    frame = self.yearLabel.frame;
    frame.origin.x = self.savingsLabel.frame.origin.x + self.savingsLabel.frame.size.width + 5;
    self.yearLabel.frame = frame;
}

#pragma mark - selectors
- (void)donePressed
{
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - Picker view data source
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return prices.count;
}

#pragma mark - Picker view delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0, 1);
    label.text = prices[row];
    float saving = [savingsCalculator savingForSubstituteAtIndex:self.subtituteIndex andPurchasePrice:[prices[row] floatValue]];
    if (saving <= 0) {
        label.accessibilityLabel = [NSString stringWithFormat:@"%@-lampan kostar %@ kronor, du sparar då %.0f kronor", self.lampString, prices[row], saving];
    } else {
        label.accessibilityLabel = [NSString stringWithFormat:@"%@-lampan kostar %@ kronor, du sparar då %.0f kronor %@", self.lampString, prices[row], saving, [self yearLabelForLampName:self.lampString]];
    }
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    [savingsCalculator setPurchasePrice:[prices[row] floatValue] forSubstituteAtIndex:self.subtituteIndex];
    self.costLabel.text = [NSString stringWithFormat:@"ca %@ kr", prices[row]];
    self.costLabel.accessibilityLabel = [self.costLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];
    self.savingsLabel.text = [NSString stringWithFormat:@"ca %.0f kr",[savingsCalculator savingForSubstituteAtIndex:self.subtituteIndex]];
    self.savingsLabel.accessibilityLabel = [self.savingsLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];

    self.yearLabel.hidden = [savingsCalculator savingForSubstituteAtIndex:self.subtituteIndex] <= 0 ? YES : NO;
    [self relayoutSavingsLabels];
}


@end
